package com.ruoyi.entity.service.impl;

import com.ruoyi.util.DownUtil;
import com.ruoyi.util.WebUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.entity.mapper.ComicMapper;
import com.ruoyi.entity.domain.Comic;
import com.ruoyi.entity.service.IComicService;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 漫画Service业务层处理
 *
 * @author Delamer
 * @date 2020-12-30
 */
@Service
public class ComicServiceImpl extends ServiceImpl<ComicMapper, Comic> implements IComicService {
    public void addComic(Comic comic) {
        baseMapper.addComic(comic);
    }

    public void setComic(Comic comic) {
        baseMapper.setComic(comic);
    }

    public Comic getComicById(Long id) {
        return baseMapper.getComicById(id);
    }

    public Comic getComicByCode(Long code) {
        return baseMapper.getComicByCode(code);
    }

    public Long getMaxId() {
        return baseMapper.getMaxId();
    }

    public void download(Long id) {
        String filepathRoot = "D:/Data/Reden/Comic/";
        String urlRoot = "https://zh.yyhentai.com/g/";
        Long target = id;
        String filepathTarget = filepathRoot + target + "/";
        String urlSingle = urlRoot + target + "/list/";
        String urlComplex = urlRoot + target + "/list2/";
        new Thread() {
            @Override
            public void start() {
                baseMapper.doDownload(id);
                //检查基础信息
                if (target == null)
                    return;
                if (!new File(filepathRoot).exists())
                    new File(filepathRoot).mkdir();
                if (!new File(filepathTarget).exists())
                    new File(filepathTarget).mkdir();
                //建立链接并获取标题
                String webResultComplex = WebUtil.GetUrl(urlComplex);
                String title = webResultComplex.substring(webResultComplex.indexOf("<title>") + 7, webResultComplex.indexOf("</title>"));
                title = title.replace("[同人誌H漫畫]", "").replace("&raquo; 喵紳士NyaHentai:免费中文A漫", "");
                System.out.print("Start ===>>> " + target + " ===>>> " + title + " ===>>> " + Calendar.getInstance().getTime() + "\n");
                //图片信息预处理
                List<String> imgUrl = new ArrayList<>();
                for (String str : webResultComplex.substring(webResultComplex.indexOf("<div id=\"to-img-1\">- 1 -</div>"), webResultComplex.indexOf("<div>- End -</div>")).split("<div id=\"to\\-img\\-\\d+\">\\- \\d+ \\-</div>")) {
                    if (str.indexOf("data-src=") > 0) {
                        String temp = str.substring(str.indexOf("data-src="));
                        temp = temp.substring(temp.indexOf("\"") + 1);
                        temp = temp.substring(0, temp.indexOf("\""));
                        imgUrl.add(temp);
                    }
                }
                List<String> imgMark = new ArrayList<String>() {{
                    addAll(imgUrl);
                }};
                //存储预览地址
                String view = "";
                for (String img : imgUrl)
                    view = view + img.substring(img.lastIndexOf("/"));
                String finalView = view.substring(1);
                baseMapper.updateById(new Comic() {{
                    setId(id);
                    setComicView(finalView);
                }});
                //开始下载
                while (imgMark.size() > 0) {
                    for (String img : imgUrl) {
                        String fileName = img.substring(img.lastIndexOf("/"));
                        String fileNameBackup = fileName.endsWith(".jpg") ? fileName.replace(".jpg", ".png") : fileName.replace(".png", ".jpg");
                        if (!new File(filepathTarget + fileName).exists() && !new File(filepathTarget + fileNameBackup).exists()) {
                            DownUtil.download(filepathTarget, img);
                            if (!new File(filepathTarget + fileName).exists())
                                DownUtil.download(filepathTarget, img.replace(fileName, fileNameBackup));
                        }
                        if (new File(filepathTarget + fileName).exists() || new File(filepathTarget + fileNameBackup).exists())
                            if (imgMark.contains(img)) {
                                imgMark.remove(img);
                                Double progress = (((imgUrl.size() - imgMark.size())) * 1.0 / (imgUrl.size() * 1.0)) * 100.0;
                                System.out.println(target + " ===>>> " + (imgUrl.size() - imgMark.size()) + " / " + imgUrl.size() + " ===>>> " + String.format("%.2f", progress) + "%");
                            }
                    }
                }
                System.out.print("End ===>>> " + target + " ===>>> " + title + " ===>>> " + Calendar.getInstance().getTime() + "\n");
                baseMapper.doComplete(id);
            }
        }.start();
    }
}
